package gajdulewiczr;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class OptionPanel extends JPanel {

	private MyGLListener listener;
	private GUI parent;
	// Fog control
	private boolean fog;
	private float fogIntensity = 30 / 1000f;
	private JCheckBox fogOn = new JCheckBox("Fog", fog);
	private JSlider fogSlider = new JSlider(0, 100, (int) (fogIntensity * 1000));
	// skybox
	private boolean skybox;
	private JCheckBox skyboxOn;
	// textures
	private boolean linear = true;
	private boolean trilinear = true;
	private JCheckBox linCB;
	private JCheckBox trilinCB;
	private boolean lodOn;
	private float lod;
	private JCheckBox lodOnCB;
	private JSlider lodS;
	// multisampling
	private boolean multis;
	private JCheckBox msCB;
	// terrain
	private boolean terrainOn;
	private JCheckBox tCB;
	private JButton tB;

	public OptionPanel(MyGLListener listener, GUI gui) {
		parent = gui;
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.listener = listener;
		this.setSize(200, 600);
		// setup fog
		fogOn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				fog = fogOn.isSelected();
				updateListener();
			}

		});
		fogSlider.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {
				fogIntensity = fogSlider.getValue() / 1000f;
				updateListener();
			}
		});
		this.add(fogOn);
		this.add(fogSlider);
		skyboxOn = new JCheckBox("Skybox", skybox);
		skyboxOn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				skybox = skyboxOn.isSelected();
				updateListener();
			}
		});
		this.add(skyboxOn);
		this.linCB = new JCheckBox("Linear Mag", linear);
		this.trilinCB = new JCheckBox("Trilinear Mipmap", trilinear);
		linCB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				linear = linCB.isSelected();
				updateListener();
			}
		});
		trilinCB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				trilinear = trilinCB.isSelected();
				updateListener();
			}
		});
		this.add(linCB);
		this.add(trilinCB);
		this.lodOnCB = new JCheckBox("Lod on", lodOn);
		this.lodOnCB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				lodOn = lodOnCB.isSelected();
				if (lodOn) {
					linCB.setSelected(false);
					linCB.setEnabled(false);
					trilinCB.setSelected(true);
					trilinCB.setEnabled(false);
					linear = false;
					trilinear = true;
					lodS.setEnabled(true);
				} else {
					linCB.setEnabled(true);
					trilinCB.setEnabled(true);
					lodS.setEnabled(false);
				}
				updateListener();
			}
		});
		this.add(lodOnCB);
		this.lod = 1;
		lodS = new JSlider(-5, 5, 1);
		lodS.setMajorTickSpacing(5);
		lodS.setPaintLabels(true);
		lodS.setEnabled(false);
		lodS.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				lod = lodS.getValue();
				updateListener();
			}
		});
		this.add(lodS);
		msCB = new JCheckBox("MultiSampling", multis);
		msCB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				multis = msCB.isSelected();
				updateListener();
			}
		});
		this.add(msCB);
		tCB = new JCheckBox("Terrain", terrainOn);
		tB = new JButton("New terrain");
		tCB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				terrainOn = tCB.isSelected();
				updateListener();
			}
		});
		tB.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				OptionPanel.this.listener.newTerrain();
				parent.requestFocus();
			}
		});
		this.add(tCB);
		this.add(tB);
	}

	private static final long serialVersionUID = 1545404981389107123L;

	private void updateListener() {
		this.listener.setFog(fog, fogIntensity);
		this.listener.setSkybox(skybox);
		this.listener.setFiltering(linear, trilinear);
		this.listener.setLod(lodOn, lod);
		this.listener.setMultiSampling(multis);
		this.listener.setTerrain(terrainOn);
		System.out.format("Fog: %b %f\n", fog, fogIntensity);
		System.out.format("Skybox: %b\n", skybox);
		System.out.format("Lin: %b Trilin: %b\n", linear, trilinear);
		System.out.format("Lod: %b %f\n", lodOn, lod);
		parent.requestFocus();
	}

}
